package set;

import java.util.*;

/**
 * @author huu
 * @create 2025-08-23
 * 需求：实现一个历史记录功能的HistorySet类
 * 步骤：使用装饰器模式
 */
public class historySet<E> implements Set<E> {
    List<E> historyList = new ArrayList<E>();
    private Set<E> delegate;
    public historySet(Set<E> delegate) {
        this.delegate = delegate;
    }

    public int size() {
        return delegate.size();
    }

    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    public boolean contains(Object o) {
        return delegate.contains(o);
    }

    public Iterator<E> iterator() {
        return delegate.iterator();
    }

    public Object[] toArray() {
        return delegate.toArray();
    }

    public <T> T[] toArray(T[] a) {
        return delegate.toArray(a);
    }

    public boolean add(E e) {
        return delegate.add(e);
    }

    public boolean remove(Object o) {
        if(delegate.remove(o)){
            historyList.add((E) o);
            return true;
        }
        return false;
    }

    public boolean containsAll(Collection<?> c) {
        return delegate.contains( c);
    }

    public boolean addAll(Collection<? extends E> c) {
        return delegate.addAll( c);
    }

    public boolean retainAll(Collection<?> c) {
        return delegate.retainAll( c);
    }

    public boolean removeAll(Collection<?> c) {
        return delegate.removeAll(c);
    }

    public void clear() {
        delegate.clear();
    }

    @Override
    public String toString() {
        return delegate.toString() + " " + historyList;
    }
}
